import axios from "axios";
import Main from "../views/Main.vue";
//import Cookies from "js-cookie";
import jwt from "jsonwebtoken"



let util = {

};



//窗口名字
util.title = function(title) {
    title = title || "后台管理系统";
    window.document.title = title;
};

//后端的地址和端口
const ajaxUrl = '/api';

util.ajax = axios.create({
    baseURL: ajaxUrl,
    timeout: 30000,
});

//post请求
util.post = function(vm, url, param, cb) {
    let axiosIns = axios.create({ //创建post请求
        baseURL: ajaxUrl,
        timeout: 30000,
        headers: {
            'Content-Type': 'application/json',
            'x-token': localStorage.getItem("token") //在请求中包含token
        }
    });
    axiosIns.post(url, param).then(res => {
        vm.loading = false;
        if (res  ) {
            //console.log(res)
            cb(res); //请求正常，执行callback函数
            return res;
        } else { //否则的话输出错误信息框
            console.log(res.data.meta);
            //if(res.data.errmsg == "登录过期，请重新登录"){
                //console.log(1111);
            localStorage.clear();
            vm.$router.push({
                name:"login"
            })
            //}
            vm.$Message.destroy();
            return res
            //vm.$Message.error(res.data.errmsg);
        }
    })
};

//get请求
util.get= function(vm,url,param,cb){
    let axiosIns = axios.create({ //创建post请求
        baseURL: ajaxUrl,
        timeout: 30000,
        headers: {
            'Content-Type': 'application/json',
            //'x-token': localStorage.getItem('token'), //在请求中包含token
            Authorization:localStorage.getItem('token')
        }
    });
    axiosIns.get(url, param).then(res => {
        //console.log(res.data);
        vm.loading = false;
        if (res && res.data.meta.status == 200) {
            console.log(res)
            cb(res.data); //请求正常，执行callback函数
        } else { //否则的话输出错误信息框
            console.log(res.data);
            vm.$Message.destroy();
            //vm.$Message.error(res.data.errmsg);
        }
    })
}
// 判断token是否有效
util.verifytoken = function(token) {
    if (token) {
        try {
            let result = jwt.verify(token, 'secret');
            return result;
        } catch (err) {
            return false;
        }
    }
    return false;
}
util.reloadMenu = function(list) { //将菜单列表转换为路由的格式，主组件都为Main，再定义子组件
    let _menuList = [];
    list.forEach((item) => {
        if (!item.children || item.children.length == 0) {
            let _item = {};
            _item.path = '/' + item.name;
            _item.icon = {type: item.icon, size:'16'};
            _item.arrow = "ios-arrow-down"
            _item.name = item.name;
            _item.title = item.title;
            _item.component = Main;
            _item.children = [{
                path: 'index',
                name: item.name + "_index",
                icon: '',
                title: item.title,
                component: resolve => { require(['@/views/' + item.href + '.vue'], resolve); }
            }];
            console.log('@/views/' + item.href + '.vue');
            _menuList.push(_item);
        } else {
            let _item = {};
            _item.path = '/' + item.name;
            _item.icon = {type: item.icon, size:'16'};
            _item.arrow = "ios-arrow-down"
            _item.name = item.name;
            _item.title = item.title;
            _item.component = Main;
            _item.children = [];
            item.children.forEach((item2) => {
                _item.children.push({
                    path: item2.name,
                    icon: {type: item2.icon, size:'16'},
                    name: item2.name,
                    title: item2.title,
                    component: resolve => { require(['@/views/' + item2.href + '.vue'], resolve); }
                });
            });
            _menuList.push(_item);
        }
    });
    return _menuList;
}


util.postAsync = function( url, param) {
    let axiosIns = axios.create({ //创建post请求
        baseURL: ajaxUrl,
        timeout: 30000,
        headers: {
            'Content-Type': 'application/json',
            'x-token': localStorage.getItem("token") //在请求中包含token
        }
    });
    return new Promise((resolve, reject) => {
        axiosIns.post(url, param).then(res => {
            if (res  ) {
                resolve(res); 
            } else { //否则的话输出错误信息框
                console.log(res.data.meta);
                resolve(res)
            }
        })
    })
        
};

util.verifyToken = (token) => {
    if (token) {
        try {
            let result = jwt.verify(token, 'secret');
            return result;
        } catch (err) {
            return false;
        }
    }
    return false;
}

export default util;